package tidudada.com.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("shopping")
public class ShoppingController {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @GetMapping("buy")
    public  String show() throws InterruptedException {
        Boolean buy = redisTemplate.opsForValue().setIfAbsent("buy", "", 100, TimeUnit.MILLISECONDS);
        if (!buy){
            return "不可操作";
        }
        String commodity = redisTemplate.opsForValue().get("commodity");
        assert commodity != null;
        if (Integer.parseInt(commodity)>= 0) {
            commodity = ((Integer.parseInt(commodity)) - 1) + "";
            redisTemplate.opsForValue().set("commodity", commodity);
        }
        return commodity+"  :" + "B";
    }




}
